# coding=utf-8
import os
import  shutil
from PIL import Image

def coverimage(src,dst,imgtype):
  img = Image.open(src)
  img.save(dst,imgtype)

def main(srcdir,destdir,todoExt=('bmp','png'),imgtype='jpeg',ignoreExt=('jpg','gif')):
  for root, dirs, files in os.walk(srcdir):
    for file in files:
      (fileName,fileExt) = os.path.splitext(file)
      fileExt = fileExt.lower()[1:]
      abfile = os.path.join(root,file)

      if fileExt in ignoreExt:
        print('copying %s : ' % abfile,end = '')
        destfile = os.path.join(destdir,file)
        shutil.copyfile(abfile,destfile)
        print('done.')
        continue
      if fileExt in todoExt:
        print('processing %s : ' % abfile,end = '')
        if imgtype == 'jpeg':
          destfile = os.path.join(destdir,fileName+'.jpg')
        else:
          destfile = os.path.join(destdir,fileName+'.'+imgtype)
        #print(destfile)
        coverimage(abfile,destfile,imgtype)
        print('done')

if __name__ == '__main__':
  path='test'
  dest='zipped'
  main(path,dest)